/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view.component;

import java.util.StringTokenizer;
import javax.faces.component.html.HtmlOutputLink;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGroup;
import offset.nodes.server.view.FacesUtils;

/**
 *
 * @author Walter Lütgenau
 */
public class PathComponent extends HtmlPanelGroup implements ContainerComponent {

    public static final String PAR_PATH = "path";

    public void initComponents() {
        String path = (String) getAttributes().get(PAR_PATH);

        if (path == null ) {
            return;
        }

        HtmlOutputText output = new HtmlOutputText();
        output.setValue("/");
        getChildren().add(output);

        HtmlOutputLink link = new HtmlOutputLink();
        link.setValue(FacesUtils.getRepositoryPath()+"/");
        getChildren().add(link);

        output = new HtmlOutputText();
        output.setValue("jcr:root");
        link.getChildren().add(output);
            
        StringBuffer currentPath = new StringBuffer();
        StringTokenizer tokenizer = new StringTokenizer(path, "/");
        
        while (tokenizer.hasMoreTokens()) {
            String pathComponent = tokenizer.nextToken();
            currentPath.append("/" + pathComponent);

            output = new HtmlOutputText();
            output.setValue("/");
            getChildren().add(output);
            
            link = new HtmlOutputLink();
            link.setValue(FacesUtils.getRepositoryPath()+currentPath);
            getChildren().add(link);
            
            output = new HtmlOutputText();
            output.setValue(pathComponent);
            link.getChildren().add(output);
        }
    }

    public String getFamily() {
        return "pathFamily";
    }
}
